package com.atxiaodei.springcloud.controller;

import com.atxiaodei.springcloud.entities.CommonResult;
import com.atxiaodei.springcloud.entities.Payment;
import com.atxiaodei.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value="/payment/create")
    public CommonResult create(Payment payment){

        int result=paymentService.create(payment);
        log.info("*********创建支付订单返回结果"+result);
        if(result>0){
            return new CommonResult(200, "创建支付订单成功,serverPort:"+serverPort,result);
        }else {
            return new CommonResult(444, "创建支付订单失败,serverPort:"+serverPort,null);
        }
    }
    @GetMapping(value="/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") long id){
        Payment payment=paymentService.getPaymentById(id);
        log.info("*****查询结果"+payment+"\t"+"测试热部署");
        if(payment!=null){
            return  new CommonResult(200, "查询成功,serverPort:"+serverPort,payment);
        }else {
            return new CommonResult(444, "数据库没有对应的记录id"+id,null);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }
    @GetMapping(value = "/payment/feign/timeout")
    public String getTimeoutFeign(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping(value="/payment/zipkin")
    public String paymentZipkin() {
        return "hello,i am paymentZipkin server fallback,O(∩_∩)O哈哈~";
    }

}
